<?php
class TDGAdministrarRepositorio{
	
	function asignarAnuncio($IDCarpeta, $IDAnuncio){
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
		
		mysql_query("UPDATE anuncio SET Clasificado = '1', IDCarpeta = '".$IDCarpeta."' WHERE ID = '".$IDAnuncio."';");		
		mysql_close($connection);
		header('Location: administrarrepositorio.php');
	}
	
	function obtenerCarpetas(){
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
		
		return mysql_query("SELECT * FROM carpeta WHERE Padre <> 'NULL' OR Padre = '0';");
	}
	
	function obtenerAnuncios(){
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
		
		return mysql_query("SELECT * FROM anuncio WHERE Activo = '1' AND Clasificado = '1';");
	}
	
	function insertarCarpeta($Nombre, $Padre){

		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
			
		mysql_query("INSERT INTO carpeta (Nombre, Padre) VALUES ('".$Nombre."', '".$Padre."');");	
		mysql_close($connection);
		header('Location: administrarrepositorio.php');
	}
	
	function actualizarNombre($ID, $Nombre){

		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
			
		mysql_query("UPDATE carpeta SET Nombre = '".$Nombre."' WHERE ID = '".$ID."';");	
		mysql_close($connection);
		header('Location: administrarrepositorio.php');
	}
	
	function actualizarDescripcion($ID, $Descripcion){

		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
			
		mysql_query("UPDATE carpeta SET Descripcion = '".$Descripcion."' WHERE ID = '".$ID."';");	
		mysql_close($connection);
		header('Location: administrarrepositorio.php');
	}
	
	function borrarCarpeta($ID){

		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		
		$unID = array();
		$unID[] = $ID;
			
		$carpetas = $this -> borrarCarpetaAux($connection, $unID, $unID);		
		$numCarpetas = count($carpetas);
		$numCarpetas--;
		
		for($i = 0; $i < count($carpetas); $i++){
		
			mysql_query("UPDATE anuncio SET IDCarpeta = null, Clasificado = 0 WHERE IDCarpeta = '".$carpetas[$numCarpetas]."';");
			mysql_query("DELETE FROM carpeta WHERE ID = '".$carpetas[$numCarpetas]."';");
			$numCarpetas--;
		}
		
		mysql_close($connection);
		header('Location: administrarrepositorio.php');
	}
	
	function borrarCarpetaAux($connection, $listaNueva, $listaTotal){
		
		$db = mysql_select_db("a83527",$connection);
		
		if(count($listaNueva) != 0){
		
			$listaActual = array();
		
			for($i = 0; $i < count($listaNueva); $i++){
			
				$listaTemp = mysql_query("SELECT * FROM carpeta WHERE PADRE = '".$listaNueva[$i]."';");
				$numrows = mysql_num_rows($listaTemp);
				
				for ( $p = 0 ; $p < $numrows ; $p ++) {
			
					$ID = mysql_result($listaTemp,$p,"ID");
					$listaActual[] = $ID;
				}
			}
			$listaTotal = array_merge($listaTotal, $listaActual);			
			return $this -> borrarCarpetaAux($connection, $listaActual, $listaTotal);
		
		}else{
			return $listaTotal;
		}
	}
	
	function obtenerAnunciosSinClasificar(){
	
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
			
		return mysql_query("SELECT * FROM anuncio WHERE Activo = '1' AND Clasificado = '0';");	
	}
	
	function obtenerAnunciosClasificados(){
	
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		if (!$connection){
            die('Error al conectarse a la base de datos:'. mysql_error());
		}
		$db = mysql_select_db("a83527",$connection);
			
		return mysql_query("SELECT * FROM anuncio WHERE Activo = '1' AND Clasificado = '1';");	
	}
	
	function buscar($texto, $campo){
		session_start();  
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		$db = mysql_select_db("a83527",$connection);
		$resultado = mysql_query("SELECT ID,NombreProyecto FROM anuncio WHERE $campo LIKE '%$texto%';");
		mysql_close($connection);
		return $resultado;
	}
	
	function filtrarFac($id){
		session_start();  
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		$db = mysql_select_db("a83527",$connection);
		$resultado = mysql_query("SELECT ID,NombreProyecto FROM anuncio WHERE Facultad= '$id';");
		ECHO $resultado;
		mysql_close($connection);
		return $resultado;
	}
	
	function filtrarUA($id){
		session_start();  
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		$db = mysql_select_db("a83527",$connection);
		$resultado = mysql_query("SELECT ID,NombreProyecto FROM anuncio WHERE UnidadAcademica=$id;");
		ECHO $resultado;
		mysql_close($connection);
		return $resultado;
	}
	
	
	function getFacultades(){
		session_start();  
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		$db = mysql_select_db("a83527",$connection);
		$resultado = mysql_query("SELECT * FROM facultad");
		mysql_close($connection);
		return $resultado;
	}
	
	function getUAs(){
		session_start();  
		$connection = mysql_connect("mysql.codefactorycr.com","a83527","patito4" );
		$db = mysql_select_db("a83527",$connection);
		$resultado = mysql_query("SELECT * FROM UA");
		mysql_close($connection);
		return $resultado;
	}
}
?>